<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部 -->
        <tree-tools :tree-node="company" :is-root="true" />
        <!-- 主要部分 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all @addDepts="addDepts">
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @addDepts="addDepts"
            @delDepts="getDepartments"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <addTools ref="addTools" :visible.sync="dialogVisible" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>

</template>

<script>
import treeTools from '@/views/departments/component/tree-tools'
import addTools from '@/views/departments/component/add-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'VueAdminTemplateMaster2Index',
  components: {
    treeTools,
    addTools
  },
  data() {
    return {
      departs: [],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {

    // 添加子部门
    async addDepts(treeNode) {
      this.node = treeNode
      console.log(this.node)
      this.dialogVisible = true
    },
    handleNodeClick(data) {
      console.log(data)
    },
    async getDepartments() {
      const res = await getDepartments()
      this.departs = tranListToTreeData(res.depts, '') // 需要将其转化成树形结构
      console.log(res)
    },

    // 编辑
    editDepts(node) {
      this.dialogVisible = true
      this.node = node // 赋值操作的节点
      this.$refs.addTools.getDepartDetail(node.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
